namespace('is.layout', new function() {
    var base = {
        //applyDefaultStyles: true,
        slidable:  false,
        resizable: false,
        spacing_open:   0,
        spacing_closed: 0,
        togglerLength_open:   0,
        togglerLength_closed: 0,
        resizerDblClickToggle: false
    };

    function resize_inner() {
        is.layout.west.resizeAll();
        is.layout.east.resizeAll();
        is.layout.body.resizeAll();
        is.layout.page.resizeAll();
        is.layout.navi.resizeAll();
    }

    this.load = function(name, title, path) {
        if(is.region.view(name) != is.region.conv(title)) {
            // Determine the layout and section of the region
            var layout  = is.region.get(name, 2);
            var section = is.region.get(name, 3);

            // Not a valid layout section
            if(layout == null) return;

            // Close the section if not already closed
            if(!layout.state[section].isClosed) layout.toggle(section);

            // Load and open the section
            is.region.load(name, title, path);
            layout.toggle(section);
        }
    }

    this.init = function() {
        // The main page layout
        is.layout.main = $('#main').layout({
            defaults: base,
            north: {
                closable: false,
                spacing_open: 10
            },
            west: {
                size: 250,
                initClosed: true,
                spacing_open: 10,
                onresize: resize_inner,
                onclose: resize_inner,
                onopen_start: is.onopen_west_all,
                spacing_closed: 0
            },
            east: {
                size: 250,
                initClosed: true,
                spacing_open: 10,
                onresize: resize_inner,
                onclose: resize_inner,
                onopen_start: is.onopen_east_all,
                spacing_closed: 0
            }
        });

        // The inner page layout, search + results
        is.layout.body = $('#body').layout({
            defaults: base,
            south: {
                size: '75%',
                initClosed: true,
                onresize: function() {
                    is.layout.body.resizeContent('center');
                    is.layout.east.resizeAll();
                    is.layout.west.resizeAll();
                },
                onresize_end: function() {
                    is.layout.page.resizeAll();
                    is.layout.navi.resizeAll();
                },
                onopen_start: is.onopen_main_all
            }
        });

        is.layout.page = $('#page').layout({
            defaults: $.extend(base, { closable: false })
        });

        // The east and west bars in the side panels
        is.layout.west = $('#west').layout({
            defaults: $.extend(base, { closable: false }),
            north: {
                size: 40
            }
        });
        is.layout.east = $('#east').layout({
            defaults: $.extend(base, { closable: false }),
            north: {
                size: 40
            }
        });

        // The menu in the top panel
        is.layout.menu = $('#menu').layout({
            defaults: $.extend(base, { closable: false }),
            west: {
                size: 300
            },
            east: {
                size: 100
            }
        });

        // The navigation bar at the top of the page panel
        is.layout.navi = $('#navi').layout({
            defaults: $.extend(base, { closable: false }),
            west: {
                size: 30
            },
            east: {
                size: 30
            }
        });
    };

    is.register('init', this);
});
